package com.ylp.sys.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.ylp.common.response.PageResult;
import com.ylp.common.response.Result;
import com.ylp.sys.auth.entity.UserInfo;
import com.ylp.sys.domain.dto.ResetPass;
import com.ylp.sys.domain.dto.UserDto;
import com.ylp.sys.domain.dto.UserPageDto;
import com.ylp.sys.domain.entity.SysUser;
import com.ylp.sys.domain.vo.UserVo;
import com.ylp.sys.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统用户表 前端控制器
 * </p>
 *
 * @author cc
 * @since 2024-07-24
 */
@Tag(name = "系统用户接口")
@RestController
@RequestMapping("/user")
public class SysUserController {
    private final SysUserService userService;

    public SysUserController(SysUserService userService) {
        this.userService = userService;
    }

    @Operation(summary = "查询当前用户信息")
    @GetMapping("/currentUser")
    public Result<SysUser> currenUser() {
        UserInfo userLoginInfo = (UserInfo) StpUtil.getSession().get("userInfo");
        Long id = userLoginInfo.getId();
        return userService.getUserById(id);
    }

    @PostMapping("/pageList")
    @Operation(summary = "查询用户列表")
    public Result<PageResult<SysUser>> pageList(@RequestBody UserPageDto userPageDto) {
        PageResult<SysUser> pageResult = userService.pageList(userPageDto);
        return Result.success(pageResult);
    }


    @Operation(summary = "删除用户")
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody List<Long> ids) {
        try {
            userService.deleteUserByIds(ids);
        } catch (Exception e) {
            return Result.error();
        }
        return Result.success();
    }

    @Operation(summary = "添加用户")
    @PostMapping("/add")
    public Result<String> add(@RequestBody UserDto userDto) {
        int result = userService.addUser(userDto);
        if (result > 0) {
            return Result.success();
        }
        return Result.error("添加失败");
    }

    @Operation(summary = "获取用户和角色ids")
    @GetMapping("/getUserAndIdsById")
    public Result<UserVo> getUserAndRoleIdById(@RequestParam Long id) {
        return Result.success(userService.getUserAndIdsById(id));
    }

    @Operation(summary = "修改用户信息")
    @PutMapping("/edit")
    public Result<String> edit(@RequestBody UserDto userDto) {
        try {
            userService.updateAndRoles(userDto);
        } catch (Exception e) {
            return Result.error();
        }
        return Result.success();
    }

    @Operation(summary = "修改密码")
    @PostMapping("/resetPass")
    public Result<String> resetPass(@RequestBody ResetPass resetPass) {
        int result = userService.resetPass(resetPass);
        if (result > 0) {
            return Result.success();
        }
        return Result.error();
    }

    @Operation(summary = "修改用户状态")
    @PutMapping("/updateStatus")
    public Result<String> updateStatus(@RequestBody SysUser user) {
        userService.updateStatus(user);
        return Result.success();
    }
}
